<template>
  <div class="dataCon">
    <div class="formdata">
      <el-form ref="form" :model="form" label-width="120px">
        <h4>一句话描述你的问题</h4>
        <el-input v-model="form.title" name="title" placeholder="请输入内容"/>
        <div class="numzi" >
          <span>0/50字</span>
        </div>
        <el-input
          :autosize="{ minRows: 6, maxRows: 10}"
          v-model="form.askCon"
          name="formcon"
          type="textarea"
          placeholder="请输入内容"
        />
        <h4>添加问题标签</h4>
        <el-tag
          v-for="tag in form.tags"
          :key="tag"
          :disable-transitions="false"
          closable
          @close="handleClose(tag)">
          {{ tag }}
        </el-tag>
        <el-input
          v-if="form.inputVisible"
          ref="saveTagInput"
          v-model="form.addTagVal"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
        <el-form-item style="margin:10px 0 0 -78px;" label="赏金" prop="delivery">
          <el-switch v-model="form.delivery" @change="usedPay"/>
          <el-input :disabled="paystst" v-model.number="form.pay" style="width:100px" />
          <span>元</span>
        </el-form-item>
        <el-row class="botBtn">
          <el-dropdown size="medium" split-button type="primary">
            同步到
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>黄金糕</el-dropdown-item>
              <el-dropdown-item>狮子头</el-dropdown-item>
              <el-dropdown-item>螺蛳粉</el-dropdown-item>
              <el-dropdown-item>双皮奶</el-dropdown-item>
              <el-dropdown-item>蚵仔煎</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <div style="float:right">
            <el-button type="danger" icon="el-icon-delete" circle/>
            <el-button type="success" icon="el-icon-check" circle @click="addForm" />
          </div>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
import { addAsk } from '@/api/ask'
export default {
  name: 'Add',
  data() {
    return {
      form: {
        title: '',
        stu_con: '',
        tags: [],
        inputVisible: false,
        delivery: false,
        addTagVal: '',
        pay: 0
      },
      paystst: true
    }
  },
  methods: {
    usedPay() {
      this.paystst = !this.paystst
    },
    handleClose(tag) {
      this.tags.splice(this.tags.indexOf(tag), 1)
    },
    showInput() {
      this.form.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const addTagVal = this.form.addTagVal
      if (addTagVal) {
        this.form.tags.push(addTagVal)
      }
      this.form.inputVisible = false
      this.form.addTagVal = ''
    },
    addForm() {
      let stu_time = Date.now()
      stu_time = Math.round(new Date().getTime() / 1000).toString()
      const formData = {
        title: this.form.title,
        stu_time: stu_time,
        stu_con: this.form.stu_con,
        pay: this.form.pay,
        tags: this.form.tags,
        token: this.$store.state.token
      }
      addAsk(formData).then((res) => {
        this.$message({
          message: res.message,
          type: 'success'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dataCon {
  width: 100%;
  height: auto;
  .formdata {
    width: 1160px;
    position: relative;
    margin: 0 auto;
    font-size: 14px;
    color: #959595;
    .numzi {
      margin: 10px;
    }
    .botBtn {
      width: 100%;
      margin-top: 30px;
    }
  }
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
